package screens {
	
	import flash.display.Shape;
	import flash.geom.Rectangle;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxPoint;
	import org.flixel.FlxSave;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	import org.flixel.FlxText;
	import org.flixel.plugin.photonstorm.*;
	
	import tools.ButtonFactory;
	import tools.MyFlxSprite;
	import tools.SaveManager;
	
	
	public class FlxPaused extends FlxGroup {
		
		
		/**
		 * Background rect for the text
		 */
		private var _bg:FlxSprite;
		
		/**
		 * The text field used to display the text
		 */
		private var _field:FlxText;
		
		/**
		 * Use this to tell if dialog is showing on the screen or not.
		 */
		public var showing:Boolean;
		
		internal var _displaying:Boolean;
		
		/**
		 * Called when dialog is finished (optional)
		 */
		private var _finishCallback:Function;
		
		public var level:FlxState;
		
		public function FlxPaused(level:FlxState) {
			this.level = level;
			var shape:FlxSprite = new FlxSprite();
			shape.makeGraphic(FlxG.width, FlxG.height, 0x88000000);
			add(shape);
			setButtons();
			setAll("scrollFactor", new FlxPoint(0, 0));
		}
		
		
		private function setButtons():void {
			
			if (!Registry.gameInfos.template.logo) {
				var mainTitle_txt:FlxText = new FlxText(10, 20, FlxG.width, Registry.gameName);
				mainTitle_txt.color = FlxG.WHITE;
				mainTitle_txt.size = 60;
				mainTitle_txt.shadow = 0xffaaaaaa;
				add(mainTitle_txt);
			} else {
				var header:MyFlxSprite = new MyFlxSprite(0,0);
				header.loadGraphicWithBitmap(Registry.gameInfos.template.logo);
				add(header);
			}
			
			var param:Array = [
				{'label' : 'Continuer',  'callBack' : hidePaused}, 
				{'label' : 'Charger', 'callBack' : displayChoices}, 
				{'label' : 'Sauvegarder', 'callBack' : SaveManager.saveGame, 'param' : 'menuSave'}, 
				{'label' : 'Quitter', 'callBack' : returnHome}
			];
			
			var buttons:ButtonFactory = new ButtonFactory(param, null, 230, Registry.gameInfos.template);
			add(buttons);
		}
			
		private function returnHome(label:String=null):void { 
			hidePaused();
			FlxG.switchState(new GameOver);
		}
		
		public function hidePaused(label:String=null):void {
			Registry.paused = false;
			if (this.level)
				this.level.setAll('active', true);
			this.kill();
			this.exists = false;
			showing = false;
			_displaying = false;
			if (_finishCallback != null)_finishCallback();
		}
		
		
		private function displayChoices(label:String=null):void{
			var saves:Object = SaveManager.getSaves();
			var param:Array = [];
			
			for (var key:String in saves){
				trace('button label :', key);
				param.push({'label' : key,  'callBack' : SaveManager.loadGame});
			}
			
			add((new FlxSprite(480,220)).makeGraphic(190, 30 * (param.length + 1), 0x55000000));
			var buttons:ButtonFactory = new ButtonFactory(param, 500, 230);
			add(buttons);
		}
		
		
		/**
		 * Call this from your code to display some dialog
		 */
		public function showPaused():void {
			Registry.paused = true;
			_displaying = true;
			showing = true;
		}
		
		
		/**
		 * The meat of the class. Used to display text over time as well
		 * as control which page is 'active'
		 */
		/*
		override public function update():void {
			if (_displaying) {
				showing = true;
				_displaying = true;
				super.update();
			}
		}*/
		
		
		/**
		 * Called when the dialog is completely finished
		 */
		public function set finishCallback(val:Function):void {
			_finishCallback = val;
		}
	
	}
}
